package nxu.service;

import com.jfinal.aop.Interceptor;
import nxu.dao.DbHelper;
import nxu.entity.User;
import org.jdbi.v3.core.Jdbi;

public class UserService {
    public boolean register(User user){
        boolean success =false;
        Jdbi jdbi = DbHelper.JDBI;

        String insertSql="insert into user(id,name,password,hometown)"+  //字段名
                "values(:uid, :uname, :password, :hometown)";            //实体类名
      try {
          jdbi.withHandle(handle -> handle.createUpdate(insertSql)
                  .bindBean(user)
                  .execute());
          success=true;
    } catch (Exception e){
          success=false;
          System.out.println("用户名重复！");
      }


        return success;
    }



    public boolean checkLogin(String uid,String password){

        String sql="select count(*) from user where id=? and password=? ";
        Integer count = DbHelper.JDBI.withHandle(handle -> handle.createQuery(sql)
                .bind(0,uid)
                .bind(1,password)
                .mapTo(Integer.class)
                .one()
        );

        return count!=0;
    }

}
